// pages/dizhi/dizhi.js
const {addAddress,shixian} =require('../../util/api')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '2016-09-01',
    time: '12:01',
    memberName:"",
    telNum:"",
    address:"",
    aaa:"",
    multiIndex:[0,0,0],
    multiArray:[],
    yData:{},
    provinceName:'',
    postalCode:'',
    cityName:'',
    cityCode:'',
    countyName:'',
    countyCode:''
  },
name(e){
  this.setData({
    memberName: e.detail.value
  })
},
tel(e){
  this.setData({
    telNum: e.detail.value
})


},
xuanze(){
  this.setData({
    aaa:0
  })
},

  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })   
  },
  address(e){
    this.setData({
      address:e.detail.value
    })
  },
dizhi(){
  if(this.data.telNum.length===11){
  let params={
    memberId:wx.getStorageSync('memberId'),
    memberName:this.data.memberName,
    telNum:this.data.telNum,
    provinceName:this.data.provinceName,
    postalCode:this.data.postalCode,
    cityName:this.data.cityName,
    countyName:this.data.countyName,
    citycode:this.data.cityCode,
    countycode:this.data.countyCode,
    detailInfo:this.data.address,
    isDefault:1
  };
  addAddress(params).then(res=>{
    if(res.code == 200)
    {
      wx.navigateBack({
        delta: 1,
      })
    }
    else
    {
      wx.showToast({
        title: res.msg,
        icon:'none'
      })
    }
    
   }
   )} else
   {
     wx.showToast({
       title: "请输入正确手机号",
       icon:'none'
     })
   }
},

bindcolumnchange(e){
  
  let multiIndex = this.data.multiIndex
  multiIndex[e.detail.column] = e.detail.value
  this.setData({
    multiIndex:multiIndex
  })
  this.changePickerData()
},

changePickerData(){
  let two = []
  let three = []
  let multiArray= this.data.multiArray
  for (let i = 0; i < this.data.yData.data[this.data.multiIndex[0]].children.length; i++) {
    two[i] = this.data.yData.data[this.data.multiIndex[0]].children[i].areaName
  }
  for (let i = 0; i < this.data.yData.data[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children.length; i++) {
    three[i] = this.data.yData.data[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children[i].areaName
  }
  multiArray[1] = two
  multiArray[2] = three
  this.setData({
    multiArray:multiArray,
    provinceName:this.data.yData.data[this.data.multiIndex[0]].areaName,
    postalCode:this.data.yData.data[this.data.multiIndex[0]].areaCode,
    cityName:this.data.yData.data[this.data.multiIndex[0]].children[this.data.multiIndex[1]].areaName,
    cityCode:this.data.yData.data[this.data.multiIndex[0]].children[this.data.multiIndex[1]].areaCode,
    countyName:this.data.yData.data[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children[this.data.multiIndex[2]].areaName,
    countyCode:this.data.yData.data[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children[this.data.multiIndex[2]].areaCode,
  })

},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    shixian().then(res=>{

      
      let first = []
      let two = []
      let three = []
      let multiArray=[]
      for (let i = 0; i < res.data.length; i++) {
        first[i] = res.data[i].areaName;
        
      }
      for (let i = 0; i < res.data[0].children.length; i++) {
        two[i] = res.data[0].children[i].areaName
      }
      for (let i = 0; i < res.data[0].children[0].children.length; i++) {
        three[i] = res.data[0].children[0].children[i].areaName
      }
      multiArray[0] = first
      multiArray[1] = two
      multiArray[2] = three
      this.setData({
        multiArray:multiArray,
        yData:res,
        provinceName:res.data[0].areaName,
        postalCode:res.data[0].areaCode,
        cityName:res.data[0].children[0].areaName,
        cityCode:res.data[0].children[0].areaCode,
        countyName:res.data[0].children[0].children[0].areaName,
        countyCode:res.data[0].children[0].children[0].areaCode,
      })
     }
     )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '分享小程序',
      imageUrl: 'https://anjingbao.anjingbao.net/profile/upload/renwufenxianginfo.jpg',
      path:"/pages/shouye/shouye",//当前页面 path ，必须是以 / 开头的完整路径
      success: (res) => {
        console.log("转发成功", res);
      },
      fail: (res) => {
        console.log("转发失败", res);
      }
    }

  }
})